package com.base.themvx.adapter

import android.view.ViewGroup
import android.widget.ImageView
import androidx.annotation.DrawableRes
import androidx.annotation.LayoutRes
import androidx.annotation.StringRes
import com.base.themvx.R
import com.base.themvx.utils.ImageUtil
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 ******************(^_^)***********************<br>
 * Author: fee(QQ/WeiXin:1176610771)<br>
 * <P>DESC:
 * 对　[BaseQuickAdapter] 进行一层的统一抽象
 * </p>
 * ******************(^_^)***********************
 */
abstract class ACommonAdapter<D, VH : BaseViewHolder>(@LayoutRes assignItemViewLayoutRes: Int = 0) :
    BaseQuickAdapter<D, VH>(assignItemViewLayoutRes, null) {

    var itemViewLayoutRes: Int = assignItemViewLayoutRes

    /**
     * 由适配器 指定 各 item 的宽
     */
    var assignItemWidth: Int? = null

    /**
     * 由适配器指定 各 item 的高
     */
    var assignItemHeight: Int? = null

    /**
     * 如果当前的列表是同一种类型的　ItemView 的布局视图，则重写之
     */
    protected open fun provideItemViewLayoutRes(): Int {
        return itemViewLayoutRes
    }

    override fun onCreateDefViewHolder(parent: ViewGroup, viewType: Int): VH {
        var theItemViewLayoutRes = this.itemViewLayoutRes
        if (theItemViewLayoutRes == 0) {
            theItemViewLayoutRes = provideItemViewLayoutRes()
            this.itemViewLayoutRes = theItemViewLayoutRes
        }
        if (theItemViewLayoutRes != 0) {
            return createBaseViewHolder(parent, theItemViewLayoutRes)
        }
        return super.onCreateDefViewHolder(parent, viewType)
    }

    protected open fun loadImg(
        ivImg: ImageView,
        imgUrl: String?,
        @DrawableRes holderResId: Int = R.drawable.shape_4_def_img,
        @DrawableRes errorResId: Int = R.drawable.shape_4_def_img
    ) {
        if (imgUrl.isNullOrBlank()) {
            return
        }
        ImageUtil.loadImage(context, imgUrl, holderResId, errorResId, ivImg)
    }

    public override fun getDefItemCount(): Int {
        return super.getDefItemCount()
    }

    fun clearDatas(){
        setList(null)
    }

    fun isEmpty() = getDefItemCount() == 0

    fun getString(@StringRes strRes: Int) = context.getString(strRes)

    //    protected void convert(@NonNull VH holder, T item, @NonNull List<? extends Object> payloads) {
    //        for (Object payload : payloads) {
    //            if (payload == PAYLOAD_SELECTED_STATE_CHANGED){
    //                onSelectedStateChanged(holder, item);
    //            }
    //        }
    //    }

    /**
     * @after onCreateDefViewHolder()
     */
    override fun bindViewClickListener(viewHolder: VH, viewType: Int) {
        super.bindViewClickListener(viewHolder, viewType)
    }

    /**
     * 选择状态变更的回调
     * @param holder VH
     * @param item D
     */
    protected open fun onSelectedStateChanged(holder: VH, item: D?) {

    }

    /**
     * 状态变更
     * @param holder VH
     * @param item D
     */
    protected open fun onStateChanged(holder: VH, item: D?){

    }
}